package com.hrpmar.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class Tools {
	public static String char2pinyini(char ch){
		HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		format.setVCharType(HanyuPinyinVCharType.WITH_V);
		String[] arr=null;
		try {
			arr = PinyinHelper.toHanyuPinyinStringArray(ch, format);
			return arr[0];
		} catch (BadHanyuPinyinOutputFormatCombination e) {
			return ch+"";
		}catch(Exception ex){
			return ch+"";
		}
	}

	public static String string2pinyin(String str){
		StringBuffer sbf = new StringBuffer();
		for(int i=0;i<str.length();i ++){
			char ch = str.charAt(i);
			sbf.append(char2pinyini(ch));
		}
		return sbf.toString();
	}
	public static void main(String[] args) {
		String arr = Tools.string2pinyin("����");
		System.out.println("ƴ��Ϊ:"+arr);
	}
}
